import re

from django_redis import get_redis_connection
from rest_framework import serializers
from rest_framework_jwt.settings import api_settings

from users.utils import generate_email_verify_url
from .models import User
from celery_tasks.email.tasks import send_verify_email



class CreateUserSerializer(serializers.ModelSerializer):
    """注册序列化器"""
    password2 = serializers.CharField(label='密码２',write_only=True)
    sms_code = serializers.CharField(label = '验证码',write_only=True)
    allow = serializers.CharField(label='同意协议',write_only=True)


    """
    序列化器中需要的所有字段：'id','username','mobile','password','password2','sms_code','allow'
    模型中已有字段：'id','username','mobile','password'
    需要进行序列化字段：'username','mobile','password','password2','sms_code','allow'
    需要进行反序列化字段：'id','username','mobile'
    
    """


    class Meta:
        model = User
        # 指定那些字段序列化
        fields = ['id','username','mobile','password','password2','sms_code','allow']

        extra_kwargs = { #　对序列化器中的字段进行额外配置
            'username':{
                'min_length':5,
                'max_length':20,
                'error_messages':{ # 自定义反序列化校验错误信息
                    'min_length':'仅允许５－２０个字符的用户名',
                    'max_length':'仅允许５－２０个字符的用户名'


                }
            },
            'password':{
                'write_only':True,  # 只做反序列化
                'min_length':8,
                'max_length':20,
                'error_messages':{
                    'min_length':'仅允许８－２０个字符的密码',
                    'max_length': '仅允许８－２０个字符的密码',

                }
            }

        }

    def validate_mobile(self,value):
        """对手机号单独追加校验逻辑"""
        if not re.match(r'^1[3-9]\d{9}',value):
            raise serializers.ValidationError('手机号码格式有误')
        return value
    def validate_allow(self,value):
        if value !='true':
            raise serializers.ValidationError('请勾选同意协议')
        return value
    def validate(self, attrs):
        # 对连个密码进行判断
        if attrs.get('password') != attrs.get('password2'):
            raise serializers.ValidationError('两次密码不一致')
        # 检验验证码
        mobile = attrs.get('mobile')
        # 创建redis 连接
        redis_conn = get_redis_connection('verify_codes')
        real_sms_code = redis_conn.get('sms_%s' % mobile)
        # redis存储数据时，都是以字符串的格式存储进行的，将来获取出来后，都会变成‘ｂｙｔｓ'类型

        if not real_sms_code:
            raise serializers.ValidationError('验证码过期')

        # 此处一定要注意从redis中取出来的字符串是bytes类型需要转换成str
        if real_sms_code.decode() != attrs.get('sms_code'):
            raise serializers.ValidationError('验证码有误')
        return attrs

    def create(self, validated_data):
        """重写序列化器的保存方法把多余数据移除"""
        del validated_data['password2']
        del validated_data['sms_code']
        del validated_data['allow']

        password = validated_data.pop('password') # 把字典中的password移除

        user = User(**validated_data)
        user.set_password(password) # 把密码进行加密后在赋值给user模型对象的password属性
        user.save()
        # return User.objects.create(**validated_data)
        # return user

        #手动生成JWT token

        # 加载生成载荷函数
        jwt_payload_handler = api_settings.JWT_PAYLOAD_HANDLER
        # 加载生成token函数
        jwt_encode_handler = api_settings.JWT_ENCODE_HANDLER


        # 通过传入user对象生成jwt载荷部分
        payload = jwt_payload_handler(user)
        # 传入payload 生成token
        token = jwt_encode_handler(payload)

        # 给user 模型对象多增加一个token属性,再给序列化器多增加token字段只做序列化
        user.token = token

        return user

class UserDetailSerializer(serializers.ModelSerializer):
    """用户信息展示序列化器"""

    class Meta:
        model = User
        fields = ['id', 'username', 'mobile', 'email', 'email_active']

class EmailSerializer(serializers.ModelSerializer):
    """设置邮箱"""

    class Meta:
        model = User
        fields = ['id', 'email']
        # 因为email字段在模型中定义时,设置的可以为空,那么ModelSerializer在自动生成字段时,
        # 如果模型中设置字段可以为空,或有默认值,那么它required默认就会生成为False
        extra_kwargs = {
            'email': {'required': True}
        }

    def update(self, instance, validated_data):
        instance.email = validated_data.get('email')
        instance.save()



        verify_url = generate_email_verify_url(instance)

        # 借此时机发个邮件玩玩
        send_verify_email.delay(instance.email, 'http://www.itcast.cn')

        return instance